package oa.struts.action;

import java.util.Collection;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import oa.domain.system.Privilege;
import oa.domain.system.Role;
import oa.domain.system.User;
import oa.service.PrivilegeService;
import oa.service.RoleService;
import oa.struts.action.base.BaseAction;
import oa.util.OAUtils;

@Controller("privilegeAction")
public class PrivilegeAction extends BaseAction<Privilege> {

	@Resource(name="privilegeService")
	private PrivilegeService privilegeService;

	@Resource(name="roleService")
	private RoleService roleService;
	private Long rid ;
	private String checkedStr;
	
	public Long getRid() {
		return rid;
	}


	public void setRid(Long rid) {
		this.rid = rid;
	}

	

	public String getCheckedStr() {
		return checkedStr;
	}


	public void setCheckedStr(String checkedStr) {
		this.checkedStr = checkedStr;
	}


	//加载权限树
	public String showPrivilegeTree(){
		
		Collection<Privilege> privilegeList = (Collection<Privilege>) this.privilegeService.getPrivilegeByRid(rid);
		System.out.println(privilegeList);
		ActionContext.getContext().getValueStack().push(privilegeList);
		return SUCCESS;
	}
	//保存角色和权限之间的关系
	public String savePrivilege(){
		Role role  = this.roleService.getRoleById(rid);
		Set<Privilege> privileges = this.privilegeService.getPrivilegeByRid(checkedStr);
		role.setPrivileges(privileges);
		this.roleService.upateRole(role);
		return SUCCESS;
	}

	 //根据用户的uid显示菜单树
	public String showMenuitemTreeByUid(){
		User user = OAUtils.getUserFromSession();
		Collection<Privilege> prilileges = this.privilegeService.getPrivilegesByUid(user.getUid(),user.getUsername());
		ActionContext.getContext().getValueStack().push(prilileges);
		return SUCCESS;
	}

	
}
